from zhdate import ZhDate as LunarDate
from datetime import datetime

from utils.handler import MethodHandler
from utils.log import Log
from utils.error import TEST_CODE


class LunarSolar(Log):
    def __init__(self, year: int, month: int, day: int, is_solar: bool = True):
        self.year = year
        self.month = month
        self.day = day
        self.is_solar = is_solar
        self._nearest()

    @MethodHandler()
    def _nearest(self):
        if not (1900 <= self.year <= 2100):
            self.error(TEST_CODE, "year only can be [1900 - 2100]")
        if not (0 < self.month <= 12):
            self.error(TEST_CODE, "month only can be [1 - 12]")
        while self.day:
            try:
                datetime(self.year, self.month, self.day) if self.is_solar else \
                    LunarDate(self.year, self.month, self.day)
                return
            except:
                self.day -= 1
                continue

    def before(self, year: int = 0, month: int = 0, day: int = 1):
        ...

    def after(self, year: int = 0, month: int = 0, day: int = 1):
        ...

    def to_solar(self, date: LunarDate):
        return date.to_datetime()

    def to_lunar(self, date: datetime):
        return LunarDate.from_datetime(date)


if __name__ == '__main__':
    d = LunarSolar(2023, 5, 32)
    d.info(d.day)

    date1 = LunarDate(2023, 5, 3)
    d.info(date1.to_datetime())

    date2 = datetime(2023, 6, 9)
    print(f"{date2.year}-{date2.month}-{date2.day}")
    date3 = LunarDate.from_datetime(date2)
    print(date3)
    print(f"{date3.lunar_year}-{date3.lunar_month}-{date3.lunar_day}")

